// PIC18F14K50 yousetu
// FileName: yousetu 14k50
#include <p18f14k50.h>
#include <delays.h>
#include <timers.h>
#include "lib_adc.h"
#pragma config CPUDIV=NOCLKDIV //CPUデバイダ÷1
#pragma config USBDIV = OFF //USBデバイダ÷1
#pragma config FOSC = HS //外部クロック使用
#pragma config PLLEN = ON //PLL×4
#pragma config PCLKEN = OFF
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF,BOREN = OFF
#pragma config WDTEN = OFF
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF
#pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF
#pragma config BORV = 30
#pragma config WDTPS = 32768
#define lcd_DB LATB
#define lcd_E LATCbits.LATC3
#define lcd_RS LATCbits.LATC4
#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)
unsigned char N, i;
unsigned int n;
void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);
void lcd_putN(unsigned int N);
void UserInit(void);
void lcd_Init(void);
void high(void);
void low(void);
#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code
void high(){
}
void low(){
if(INTCON3bits.INT1IF){
PORTCbits.RC0 = 1; // 50Hzの N波数分の出力
for(i=0; i<N; i++) Delay_10mS ;
PORTCbits.RC0 = 0; // 出力を止める
Delay_100mS;Delay_100mS;Delay_100mS;
Delay_100mS;Delay_100mS;
INTCON3bits.INT1IF = 0;// 割り込みフラグを0に戻す
}
}
void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay1TCY();
lcd_E = 0;
}
void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}
void lcd_clr(void){
lcd_c4(0x01);
}
void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}
void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}
void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}
void lcd_putN(unsigned int N){
unsigned char buf[4];
for(i=0; i<4; i++) buf[i] = ' ';
i=3;
do{
buf[i]=(N%10)+'0';
N=N/10;
i--;
} while(N>0);
for(i=0; i<4; i++) lcd_putchr(buf[i]);
}
void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}
void UserInit(void){
N = 0;
PORTCbits.RC0 = 0;
}
char line11[] = "Spot";
char Wav[] = " Wav";
char NN[] = "N= ";
char line12[] = "Continue";
char blank[] = " ";
void main(void){
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b10100010; // RC1、5、7は入力用
ANSEL = 0b00000000;
ANSELH = 0b00000010; // AN9のみアナログ入力
INTCON3bits.INT1IE = 0;// int1 割り込み 1で有効
INTCON3bits.INT1IP = 0;// 低優先
INTCON3bits.INT1IF = 0;// フラグ
RCONbits.IPEN = 1;// 高優先・低優先 2段階
INTCONbits.GIEH = 1;// 高優先割込み 許可
INTCONbits.GIEL = 1;// 低優先割込み 許可
lcd_init();
UserInit();
while(1){
Delay_20mS;
if(PORTCbits.RC5 == 0){
INTCON3bits.INT1IE = 1;// 割り込みを有効にする
PORTCbits.RC0 = 0;
OpenADC(ADC_FOSC_4 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VDD &
ADC_REF_VDD_VSS,
ADC_CH9 &
ADC_INT_OFF );
ConvertADC();
while(BusyADC());
n=(ADRESH*256) + ADRESL;
N = n/20; // 0から14までの数とする
lcd_clr();
lcd_locate(0,0);
lcd_puts(line11);
lcd_puts(Wav);
lcd_locate(1,0);
lcd_puts(NN);
lcd_putN(N);
Delay_100mS;Delay_100mS;
} else {
INTCON3bits.INT1IE = 0;// 割り込みを無効にする
PORTCbits.RC0 = 1;// 常時出力
lcd_clr();
lcd_locate(0,0);
lcd_puts(line12);
lcd_locate(1,0);
lcd_puts(blank);
Delay_100mS;Delay_100mS;
}
}
}